package auto.app.service.impl;

import auto.app.controller.from.VariableAdd;
import auto.app.controller.from.VariableUpdate;
import auto.app.exetion.MainExection;
import auto.app.model.VariableCenter;
import auto.app.dao.VariableCenterDao;
import auto.app.service.VariableCenterService;
import auto.app.util.CommonUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

import static auto.app.exetion.MainExetionMessage.VARIABLECENTEREMPTY;
import static auto.app.exetion.MainExetionMessage.VARIABLECENTNOTEREMPTY;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 杨森
 * @since 2022-08-17
 */
@Service
public class VariableCenterServiceImpl extends ServiceImpl<VariableCenterDao, VariableCenter> implements VariableCenterService {

    @Override
    public Boolean addVariable(VariableAdd variableAdd) throws MainExection {
        //先查询变量名是否存在
        LambdaQueryWrapper<VariableCenter> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(VariableCenter::getVariableName, variableAdd.getVariableName());
        VariableCenter variableCenter = baseMapper.selectOne(lambdaQueryWrapper);
        if (variableCenter != null) {
            throw new MainExection(VARIABLECENTNOTEREMPTY.getCode(), VARIABLECENTNOTEREMPTY.getMsg());
        }
        //新增
        VariableCenter variableCenter1 = CommonUtil.copyOne(variableAdd, VariableCenter.class);
        variableCenter1.setCreateTime(LocalDateTime.now());
        int insert = baseMapper.insert(variableCenter1);
        return true;
    }

    @Override
    public VariableCenter detail(Integer variableId) throws MainExection {
        //先查询变量是否存在
        VariableCenter variableCenter = baseMapper.selectById(variableId);
        if (variableCenter == null) {
            throw new MainExection(VARIABLECENTEREMPTY.getCode(), VARIABLECENTEREMPTY.getMsg());
        }
        return variableCenter;
    }

    @Override
    public Boolean updateVariable(VariableUpdate variableUpdate) throws MainExection {
        //判断变量是否存在
        VariableCenter variableCenter1 = baseMapper.selectById(variableUpdate.getVariableId());
        if (variableCenter1 == null) {
            throw new MainExection(VARIABLECENTEREMPTY.getCode(), VARIABLECENTEREMPTY.getMsg());
        }
        //先查询变量名是否存在
        LambdaQueryWrapper<VariableCenter> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(VariableCenter::getVariableName, variableUpdate.getVariableName());
        VariableCenter variableCenter = baseMapper.selectOne(lambdaQueryWrapper);
        //判断变量重名时 排除自己情况
        if (variableCenter != null) {
            if (variableCenter.getVariableId() != variableUpdate.getVariableId()) {
                throw new MainExection(VARIABLECENTNOTEREMPTY.getCode(), VARIABLECENTNOTEREMPTY.getMsg());
            }
        }
        VariableCenter variableCenter2 = CommonUtil.copyOne(variableUpdate, VariableCenter.class);
        int i = baseMapper.updateById(variableCenter2);
        return true;
    }
}
